package mfuny.ggongal.admin.notice;

import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class NoticeService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public NoticeService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
	}
	
	//공지사항 리스트.
	@SuppressWarnings( "unchecked" )
	public ModelAndView noticeList() throws Exception {
		List<HashMap<String, Object>> noticeList = sqlMapper.queryForList( "notice_selectSubject", 0 );
		mav.clear( );
		mav.setViewName( "/notice/noticeList.ga" );
		mav.addObject( "noticeList", noticeList );
		return mav;
	}
	
	//공지사항 등록.
	public ModelAndView regNoticeForm() throws Exception {
		mav.clear( );
		mav.setViewName( "/notice/regNoticeForm.ga" );
		return mav;
	}
	
	//공지사항 글 등록 처리.
	public void regNotice( NoticeDao noticeClass ) throws Exception {
		noticeClass.setContent(noticeClass.getContent().replaceAll("\r\n", "\\\\n"));
		sqlMapper.update( "notice_insert", noticeClass );
	}
	
	//공지사항 글 보기.
	public ModelAndView readNotice( int notice_no ) throws Exception {
		NoticeDao noticeClass = (NoticeDao) sqlMapper.queryForObject( "notice_selectByNo", notice_no );
		noticeClass.setContent(noticeClass.getContent().replaceAll("\\\\n", "\n"));
		mav.clear( );
		mav.setViewName( "/notice/readNotice.ga" );
		mav.addObject( "noticeClass", noticeClass );
		return mav;
	}
	
	//공지사항 글 삭제 팝업.
	public ModelAndView deleteNoticePopup( int notice_no ) throws Exception {
		mav.clear( );
		mav.setViewName( "/notice/deleteNoticePopup" );
		mav.addObject( "notice_no", notice_no );
		return mav;
	}
	
	//공지사항 글 삭제 처리.
	public void deleteNotice( int notice_no ) throws Exception {
		sqlMapper.delete( "notice_delete", notice_no );
	}
	
	//공지사항 글 수정폼.
	public ModelAndView modifyNoticeForm( int notice_no ) throws Exception {
		NoticeDao noticeClass = (NoticeDao) sqlMapper.queryForObject( "notice_selectByNo", notice_no );
		noticeClass.setContent(noticeClass.getContent().replaceAll("\\\\n", "\n"));
		mav.clear( );
		mav.setViewName( "/notice/modifyNoticeForm.ga" );
		mav.addObject( "noticeClass", noticeClass );
		return mav;
	}
	
	//공지사항 글 수정처리.
	public void modifyNotice( NoticeDao noticeClass ) throws Exception {
		noticeClass.setContent(noticeClass.getContent().replaceAll("\r\n", "\\\\n"));
		sqlMapper.update( "notice_update", noticeClass );
	}
}
